﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Resources;
using System.IO;
using CommonExtension;
using System.ComponentModel;
using System.Windows.Data;
using System.Globalization;

namespace MaskedEditDemo
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            LblXaml.Text = ReadResourceFile("MainPage.xaml");
            LblCode.Text = ReadResourceFile("MaskEditDemo.Code");
            ScrllXaml.SetIsMouseWheelScrollingEnabled(true);
            ScrllCode.SetIsMouseWheelScrollingEnabled(true);
            lblcurrLng.Text = "Currency Number (" + System.Threading.Thread.CurrentThread.CurrentCulture.Name + ")";
            //twoway sample
            ClsTwoWaySample objDate = new ClsTwoWaySample();
            objDate.Year = DateTime.Now.Year;
            objDate.Age = 0;
            MaskYear.DataContext = objDate;
            MaskAge.DataContext = objDate;
        }

        private string ReadResourceFile(string FileName)
        {
            StreamResourceInfo sri = Application.GetResourceStream(
                new Uri(this.GetType().Namespace + ";component/" + FileName, UriKind.Relative));
            string res = null;
            if (sri != null)
            {
                using (StreamReader sr = new StreamReader(sri.Stream, System.Text.Encoding.UTF8))
                {
                    res = sr.ReadToEnd();
                }
            }
            return res;
        }

        private void BntSelectAll_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (TabCode.SelectedIndex == 0)
			{
                LblXaml.Focus();
                LblXaml.Select(0, LblXaml.Text.Length);
			}
			else	
			{
                LblCode.Focus();
				LblCode.Select(0,LblCode.Text.Length);
			}
        }

        private void MaskNum_LostFocus(object sender, RoutedEventArgs e)
        {
            LblNumUnMask.Text = this.MaskNum.TextUnMasked;
        }

        private void MaskText_LostFocus(object sender, RoutedEventArgs e)
        {
            LblTextUnMask.Text = this.MaskText.TextUnMasked;
        }

        private void MaskCus_LostFocus(object sender, RoutedEventArgs e)
        {
            LblCusUnMask.Text = this.MaskCus.TextUnMasked;
        }

        private void MaskPrd_LostFocus(object sender, RoutedEventArgs e)
        {
            LblPrdUnMask.Text = this.MaskPrd.TextUnMasked;
        }

        private void MaskUcase_LostFocus(object sender, RoutedEventArgs e)
        {
            LblUcaseUnMask.Text = this.MaskUcase.TextUnMasked;
        }

        private void MaskCurr_LostFocus(object sender, RoutedEventArgs e)
        {
            LblCurrUnMask.Text = this.MaskCurr.TextUnMasked;
        }

        private void ChkFixRight_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
        	this.MaskCurr.FixedCursorRightPosition = true;
            this.LblCurrUnMask.Text = "";
            this.MaskCurr.Reload();
        }

        private void ChkFixRight_Unchecked(object sender, System.Windows.RoutedEventArgs e)
        {
        	this.MaskCurr.FixedCursorRightPosition = false;
            this.LblCurrUnMask.Text = "";
            this.MaskCurr.Reload();
        }
    }

    public class MaskEditConvertNumbers : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //NO CONVERTER
            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string result = value.ToString();
            result = result.Replace("_", "");
            return result;
        }

        #endregion
    }

    public class ClsTwoWaySample : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private int _intYear = -1;
        private int _intAge = -1;
        public int Year
        {
            set
            {
                if (_intYear != value)
                {
                    _intYear = value;
                    OnPropertyChanged("Year");
                    Age = DateTime.Now.Year - _intYear;
                }
            }
            get
            {
                return _intYear;
            }

        }
        public int Age
        {
            set
            {
                if (_intAge != value)
                {
                    _intAge = value;
                    OnPropertyChanged("Age");
                    Year = DateTime.Now.Year - _intAge;
                }
            }
            get
            {
                return _intAge;
            }
        }
        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                      new PropertyChangedEventArgs(property));
            }
        }
    }

}
